package com.white.androidstudy.ipc;

import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.SystemClock;
import android.support.annotation.IntDef;
import android.util.Log;

import com.white.androidstudy.aidl.Book;
import com.white.androidstudy.aidl.IBookArrivedListener;
import com.white.androidstudy.aidl.IBookManager;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

public class BookManagerService extends Service
{
    private AtomicBoolean hasDestroyed = new AtomicBoolean(false);

    public BookManagerService()
    {
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        int res = checkCallingOrSelfPermission("com.white.androidstudy.bs");
        if(res == PackageManager.PERMISSION_GRANTED)
        {
            return myBinder;
        }
        return null;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        list.add(new Book(1, "安卓开发艺术探索"));
        list.add(new Book(2, "安卓底层开发"));
        new Thread()
        {
            @Override
            public void run()
            {

                SystemClock.sleep(1000);
                while (!hasDestroyed.get())
                {
                    newBookWithNotification();
                    SystemClock.sleep(5000);
                }
            }
        }.start();
    }

    private void newBookWithNotification()
    {
        int siz = list.size()+1;
        Book b = new Book(siz, "新书"+siz);
        list.add(b);
        Log.e("s", "添加了一本新书");
        int n = listeners.beginBroadcast();
        for(int i = 0;i<n;i++)
        {
            IBookArrivedListener is = listeners.getBroadcastItem(i);
            if(is != null)
            {
                try
                {
                    is.bookArrived(b);
                } catch (RemoteException e)
                {
                    e.printStackTrace();
                }
            }
        }
        listeners.finishBroadcast();
    }

    CopyOnWriteArrayList<Book> list = new CopyOnWriteArrayList<>();
    RemoteCallbackList<IBookArrivedListener> listeners = new RemoteCallbackList<>();

    private Binder myBinder = new IBookManager.Stub()
    {
        @Override
        public List<Book> getBookList() throws RemoteException
        {
            return list;
        }

        @Override
        public void addBook(Book b) throws RemoteException
        {
            list.add(b);
        }

        @Override
        public void addOnBookArrivedListener(IBookArrivedListener l) throws RemoteException
        {
            listeners.register(l);
        }

        @Override
        public void removeOnBookArrivedListener(IBookArrivedListener l) throws RemoteException
        {
            listeners.unregister(l);
        }
    };

    @Override
    public void unbindService(ServiceConnection conn)
    {
        super.unbindService(conn);
        Log.e("s", "调用unbindService");
        hasDestroyed.set(true);
        stopSelf();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        hasDestroyed.set(true);
    }
}
